<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>map和reduce</h1>


<script>

    //1.map
    /*
    let pow=(x)=>x*x;
    let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    let results = arr.map(pow); // [1, 4, 9, 16, 25, 36, 49, 64, 81]
    console.log(results);*/

    //2.map函数里也可以传入匿名函数
    const materials = [
        'Hydrogen',
        'Helium',
        'Lithium',
        'Beryllium'
    ];

    console.log(materials.map(material => material.length));

    //3.reduce函数是将数组的前两个元素做运算，将运算的结果再与第三个元素做运算，以此类推运算到最后一个元素得出最后的结果。
    const arr = [1, 3, 5, 7, 9];
    let result = arr.reduce(function (x, y) {
        return x + y;
    });

    console.log("sum="+result);
</script>
</body>
</html>